#!/usr/bin/env python
# encoding: utf-8
'''
Adb.AdbHelper -- shortdesc

Adb.AdbHelper is a description

It defines classes_and_methods

@author:     Anson Chan

@copyright:  2016 Xerland Co. Ltd. All rights reserved.

@contact:    anson_chan@xerland.com
@deffield    updated: Updated
'''
from Utils.SysConstants import *
import win32com.client
from Utils.Singleton import Singleton
from Utils.adoconstants import *
import threading
import pythoncom

class AdbHelper(Singleton):
    '''
    classdocs
    '''

    def __init__(self):
        '''
        Constructor
        '''
        conf = SERVERAPI_CONFIGSERVICE
        self.DSN = "PROVIDER=Microsoft.Jet.OLEDB.4.0;\
        DATA SOURCE=%s;\
        Persist Security Info=False;\
        Jet OLEDB:Database Password=%s"%(
                                         conf.getValue("db_source"),
                                         conf.getValue("db_code")                               
                                         )
        self.conn = None
        
    def getConn(self):
        if threading.currentThread ().getName () <> 'MainThread':
           pythoncom.CoInitialize ()        
        if not self.conn:
            self.conn = win32com.client.Dispatch(r'ADODB.Connection')
        self.conn.Open(self.DSN)
        return self.conn
    
    def close(self):
        if self.conn.State == adStateOpen:
            self.conn.Close()
        if threading.currentThread ().getName () <> 'MainThread':
            pythoncom.CoUninitialize ()            
        
if __name__ == "__main__":
    adb = AdbHelper()
    adb.getconn()
    adb.close()
     